package com.ruoyi.asms.controller;

import com.ruoyi.asms.domain.ProxyInfo;
import com.ruoyi.asms.service.IProxyInfoService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.security.annotation.PreAuthorize;
import com.ruoyi.system.controller.SysUserController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

/**
 * 代理信息Controller
 *
 * @author yfkj
 * @date 2021-10-21
 */
@RestController
@RequestMapping("/asms/ProxyInfo")
public class ProxyInfoController extends BaseController {
    @Autowired
    private IProxyInfoService proxyInfoService;
//    @Autowired
//    private RemoteUserService remoteUserService;
    @Autowired
    private SysUserController userController;

    /**
     * 查询代理信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:ProxyInfo:list")
    @GetMapping("/list")
    public TableDataInfo list(ProxyInfo proxyInfo) {
        startPage();
        List<ProxyInfo> list = proxyInfoService.selectProxyInfoList(proxyInfo);
        return getDataTable(list);
    }

    /**
     * 导出代理信息列表
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:ProxyInfo:export")
    @Log(title = "代理信息", businessType = BusinessType.EXPORT)
    @PostMapping(value="/export", produces = "application/octet-stream")
    public void export(HttpServletResponse response, ProxyInfo proxyInfo) throws IOException {
        List<ProxyInfo> list = proxyInfoService.selectProxyInfoList(proxyInfo);
        ExcelUtil<ProxyInfo> util = new ExcelUtil<ProxyInfo>(ProxyInfo.class);
        util.exportExcel(response, list, "代理信息数据");
    }

    /**
     * 获取代理信息详细信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:ProxyInfo:query")
    @GetMapping(value = "/{proxyInfoId}")
    public AjaxResult getInfo(@PathVariable("proxyInfoId") Long proxyInfoId) {
        return AjaxResult.success(proxyInfoService.selectProxyInfoByProxyInfoId(proxyInfoId));
    }

    /**
     * 新增代理信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:ProxyInfo:add")
    @Log(title = "代理信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProxyInfo proxyInfo) {
        return toAjax(proxyInfoService.insertProxyInfo(proxyInfo));
    }

    /**
     * 修改代理信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:ProxyInfo:edit")
    @Log(title = "代理信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProxyInfo proxyInfo) {
        return toAjax(proxyInfoService.updateProxyInfo(proxyInfo));
    }

    /**
     * 删除代理信息
     */
    @PreAuthorize
    //@PreAuthorize(hasPermi = "asms:ProxyInfo:remove")
    @Log(title = "代理信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{proxyInfoIds}")
    public AjaxResult remove(@PathVariable Long[] proxyInfoIds) {
        return toAjax(proxyInfoService.deleteProxyInfoByProxyInfoIds(proxyInfoIds));
    }

    @GetMapping("/testACID")
    @Transactional
    public AjaxResult testACID() {
        //查通知的用户id，用户通知方式
//        Map<Long,Set<Integer>> noticeInfoMap =new HashMap<>();
//        Set<Long> toWxUserIds=new HashSet<>();
//        Set<Long> toEmailUserIds=new HashSet<>();
//        Set<Long> toPhoneUserIds=new HashSet<>();
//        noticeInfoMap.forEach((userId, types) -> {
//            types.forEach(type -> {
//                switch (type){
//                    case 1:toWxUserIds.add(userId);break;
//                    case 2:toEmailUserIds.add(userId);break;
//                    case 3:toPhoneUserIds.add(userId);break;
//                }
//            });
//        });
//        wxMessageService.sendWxMsgtoUsers(toWxUserIds,content,WxMessageService.DEFAULT_TEMPT);
//        emailMessageService.sendEmailMsgtoUsers(toEmailUserIds,content,EmailMessageService.DEFAULT_TEMPT);
//        phoneMessageService.sendPhoneMsgtoUsers(toPhoneUserIds,content,PhoneMessageService.DEFAULT_TEMPT);


        SysUser user = new SysUser();
        user.setUserId(1L);
        user.setNickName("焱飞" + DateUtils.dateTimeNow());
        R r = userController.testACID(user);
        if (R.FAIL != r.getCode()) {
            ProxyInfo proxyInfo = new ProxyInfo();
            proxyInfo.setProxyDeptId(1L);
            //proxyInfo.setProxyInfoId(1L);
            proxyInfo.setProxyInfo("修改成功！" + DateUtils.dateTimeNow());
            proxyInfoService.save(proxyInfo);
        }
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(Calendar.SECOND);
        if ((i & 1) == 1) {
            return AjaxResult.success("成功！");
        }else {
            throw new GlobalException("出错");
        }

    }
}
